home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / tcomm50.zip / QBTTL.C < prev    next >
Text File  |  1989-07-31  |  25KB  |  1,101 lines

  1. /*
  2. **  QBTTL is a complete, if somewhat limited, terminal emulation
  3. **  program  designed to demonstrate the use of the LiteComm(tm)
  4. **  ToolBox.  The executable version is included so that you can
  5. **  try it out while viewing the code.  To successfully create a
  6. **  new version of QBTTL, you must have the XMODEM and WXMODEM engines
  7. **  which are provided as part of your registration package.  The
  8. **  QUICKB functionality is derived from public domain code, and adapted
  9. **  by Information Technology for use with LiteComm.
  10. **  Also note that the windowing parts of the program are based upon
  11. **  Vitamin-C by Creative Programming.  You must have this product and
  12. **  be a registrant to successfully compile a new version of QBTTL.
  13. **
  14. **  While non-registered users cannot create a new version of QBTTL, as-is
  15. **  we have provided the source as part of the distribution package to
  16. **  help you in your understanding of the way in which the LiteComm
  17. **  Communications ToolBox can be used.
  18. **
  19. **  QBTTL functions as a vidtex terminal when used in conjuction with
  20. **  CompuServe.  In addition, it will run on any communications port from
  21. **  1 thru 4, defaulting to port 2 (COM2).  To execute QBTTL on other than
  22. **  COM2, specify
  23. **    QBTTL n
  24. **  where n is a number from 1 to 4.
  25. **
  26. **  Please note also that, to send a Ctrl-C, you must use the Alt-C keys
  27. **
  28. **  Information Technology, Ltd.
  29. */
  30.  
  31. #include "litecomm.h"
  32. #include "litexm.h"
  33. #include <vcstdio.h>
  34.  
  35. #ifdef __TURBOC__
  36. #include <stdlib.h>
  37. #include <dos.h>
  38. #include <fcntl.h>
  39. #include <sys\stat.h>
  40. #include <io.h>
  41. #include <mem.h>
  42. #endif
  43.  
  44. #ifdef M_I86
  45. #include <signal.h>
  46. #include <conio.h>
  47. #include <ctype.h>
  48. #include <fcntl.h>
  49. #include <stdio.h>
  50. #include <sys\types.h>
  51. #include <sys\stat.h>
  52.  
  53. #endif
  54.  
  55. #ifdef __TURBOC__
  56. extern unsigned _stklen = 8192;
  57. #endif
  58.  
  59. #define CTRLX 0x18
  60.  
  61. #define ESC 0x1b
  62. #define ENQ 0x05
  63.  
  64. int nocbrk();
  65. void moveleft();
  66. void moveright();
  67. void moveup();
  68. void movedn();
  69. void show_modem();
  70. void simtab();
  71. void setcursorpos();
  72. void procesc();
  73. void clreos();
  74. void send(void);
  75. void recv(void);
  76. void xupload(void);
  77. void xdnload(void);
  78. void wupload(void);
  79. void wdnload(void);
  80.  
  81. /*
  82. **  Comm parameters - human-readable form
  83. */
  84. int baud = 2400;
  85. char parity = 'N';
  86. int data = 8;
  87. int stop = 1;
  88.  
  89. /*
  90. **  Comm parameters - Litecomm form
  91. */
  92. unsigned pbaud = 2400;
  93. unsigned pparity = NPARITY;
  94. unsigned pbits = BIT8;
  95. unsigned pstop = STOP1;
  96.  
  97. unsigned port = 2;
  98.  
  99. int xmode = 0;                          /* xmodem type */
  100. int yxmode = 0;                            /* use small blocks */
  101. int halfd = 0;                          /* half-duplex */
  102. int hostm = 0;                          /* host mode */
  103. int ctlc_hit = FALSE;
  104.  
  105. char MODEMSET0[] = "ATZ\r";
  106. char MODEMSET1[] = "ATT E0\r";
  107. char MODEMSET2[] = "ATC1 V0 X1 S0=1 M1\r";
  108.  
  109. COUNT mwin;                             /* for Vitamin-C */
  110. COUNT swin;
  111.  
  112. COUNT mbd;
  113. COUNT mbg;
  114. COUNT msay;
  115. COUNT mact;
  116. COUNT mnact;
  117. COUNT mtitle;
  118. COUNT sbd;
  119. COUNT sbg;
  120. COUNT ssay;
  121. COUNT sact;
  122. COUNT snact;
  123. COUNT stitle;
  124. char    strbuf[80];                   /* for sprintf usage */
  125.  
  126. void main(argc, argv)
  127. int    argc;
  128. char *argv[];
  129. {
  130.     COUNT    opt;
  131.  
  132. #ifdef M_I86
  133.     signal(SIGINT, nocbrk);               /* set Ctrl-Break handler */
  134. #endif
  135.  
  136. #ifdef __TURBOC__
  137.     ctrlbrk(nocbrk);
  138. #endif
  139.  
  140. /*
  141. **  check for a port parameter
  142. */
  143.     if (argc > 1)
  144.     {
  145.         port = atoi(argv[1]);
  146.         if ((port < 1) || (port > 4))
  147.         {
  148.             puts("Invalid Port Specified\n");
  149.             exit(4);
  150.         }
  151.     }
  152.  
  153. /*
  154. ** establish windowing environment
  155. */
  156.     vcstart(SAVESCRN);
  157.     VC_VIO = 1;
  158.     if ((mwin=wxopen(0,0,23,79,NULL,
  159.          ACTIVE+CURSOR+SCROLL+COOKED+NOADJ+CENTER, 0, 0)) == -1)
  160.         terror("mwin:Not Enough Memory");
  161.     if ((swin=wxopen(24,0,24,79,NULL,
  162.          ACTIVE+COOKED+NOADJ, 0, 0)) == -1)
  163.         terror("swin:Not Enough Memory");
  164.     wselect(mwin);
  165.  
  166. /*
  167. ** get display attributes
  168. */
  169.     wattr(mwin,&mbd, &mbg, &msay, &mact, &mnact, &mtitle, GET);
  170.     wattr(swin,&sbd, &sbg, &ssay, &sact, &snact, &stitle, GET);
  171.  
  172. /*
  173. ** set-up the comm port
  174. */
  175.     if (comm_opn(port,2400,NPARITY,BIT8,STOP1,2048,2048,FALSE) == -1)
  176.     {
  177.         urgentmsg("ERROR", "Can't open port");
  178.         abort();
  179.     }
  180.     lc_setmdm(port, (DTR | RTS));
  181.  
  182.     while (1)
  183.     {
  184.         erase();
  185.         at(0,0);
  186.         vcputs("-- MAIN MENU --\r", msay);
  187.         vcputs("T - enter Terminal mode\r", msay);
  188.         vcputs("    Alt-X leaves terminal mode\r", msay);
  189.         sprintf(strbuf, "H - toggle Host mode (now %s)\r",hostm ? "ON":"OFF");
  190.         vcputs(strbuf, msay);
  191.         sprintf(strbuf, "G - toGgle half-duplex mode (now %s)\r",halfd ? "ON":"OFF");
  192.         vcputs(strbuf, msay);
  193.         sprintf(strbuf, "C - change Comm parameters (now %d,%c,%d,%d)\r",
  194.                 baud,parity,data,stop);
  195.         vcputs(strbuf, msay);
  196.         sprintf(strbuf, "X - change Xmodem mode (now %s)\r",xmode ? "WIN":"NOR");
  197.         vcputs(strbuf, msay);
  198.         sprintf(strbuf, "Y - toggle Ymodem mode (now %s)\r",yxmode ? "ON":"OFF");
  199.         vcputs(strbuf, msay);
  200.         vcputs("S - Send a file\r", msay);
  201.         vcputs("R - Receive a file\r", msay);
  202.         vcputs("Q - Quit\r\r", msay);
  203.  
  204.         opt = getone();
  205.         opt = toupper(opt);
  206.  
  207.         switch (opt)
  208.         {
  209.             case 'T':   terminal();
  210.                         break;
  211.             case 'H':   if (hostm)
  212.                             hostm = 0;
  213.                         else
  214.                         {
  215.                             hostm = 1;
  216.                             halfd = 0;
  217.                         }
  218.                         break;
  219.             case 'G':   if (halfd)
  220.                             halfd = 0;
  221.                         else
  222.                         {
  223.                             halfd = 1;
  224.                             hostm = 0;
  225.                         }
  226.                         break;
  227.             case 'X':   if (xmode)
  228.                             xmode = 0;
  229.                         else
  230.                             xmode = 1;
  231.                         break;
  232.             case 'Y':   if (yxmode)
  233.                         {
  234.                             ymodem = FALSE;
  235.                             yxmode = 0;
  236.                         }
  237.                         else
  238.                         {
  239.                             yxmode = 1;
  240.                             ymodem = TRUE;
  241.                         }
  242.                         break;
  243.             case 'S':   send();
  244.                         break;
  245.             case 'R':   recv();
  246.                         break;
  247.             case 'C':   chgcomm();
  248.                         break;
  249.         }
  250.         if (opt == 'Q')
  251.             break;                      /* shut down time */
  252.     }                                   /* while (1) */
  253.  
  254.     comm_close(port,FALSE);
  255.     vcend(CLOSE);
  256.     exit(0);
  257. }                                       /* main */
  258.  
  259. int nocbrk()
  260. {
  261. #ifdef M_I86
  262.     signal(SIGINT, SIG_IGN);             /* set Ctrl-Break handler */
  263. #endif
  264.     ctlc_hit = TRUE;
  265.     _abort_flag = TRUE;
  266. #ifdef M_I86
  267.     signal(SIGINT, nocbrk);             /* set Ctrl-Break handler */
  268. #endif
  269.     return(TRUE);
  270. }
  271.  
  272. terminal()
  273. {
  274.     COUNT ch;
  275.     COUNT row, col;
  276.     COUNT twin;
  277.  
  278.     erase();
  279.     lc_xoff(port,TRUE);
  280.  
  281.     while (1)
  282.     {
  283.         if ((ch = lc_get(port)) != -1)
  284.             switch (ch & 0x7f)
  285.             {
  286.                 case 0x08:             /* BS */
  287.                     moveleft();
  288.                     vcputc(' ', msay);
  289.                     moveleft();
  290.                     break;
  291.                 case '\t':                /* HT */
  292.                     simtab();
  293.                     break;
  294.                 case '\r':              /* CR */
  295.                     wcurspos(mwin, &row, &col);
  296.                     at(row,0);
  297.                     break;
  298.                 case DLE:
  299.                     twin=wxopen(6,20,13,60,"TRANSFER A FILE",
  300.                         BORDER+BD1+ACTIVE+CURSOR+SCROLL+COOKED+NOADJ+CENTER, 0, 0);
  301.                     wselect(twin);
  302.                     bp_DLE();
  303.                     wclose(twin);
  304.                     wselect(mwin);
  305.                     break;
  306.                 case ENQ:
  307.                     bp_ENQ();
  308.                     break;
  309.                 case ESC:
  310.                     procesc();
  311.                     break;
  312.                 default:
  313.                     if (pparity != NPARITY)
  314.                         ch &= 0x7f;                /* strip parity */
  315.                     vcputc(ch, msay);
  316.                     if (hostm)
  317.                     {
  318.                         lc_put(port,ch);            /* echo back */
  319.                         if (ch == '\r')             /* was it return */
  320.                         {
  321.                             lc_put(port,'\n');      /* echo lf as well */
  322.                             vcputc('\n', msay);
  323.                         }
  324.                     }
  325.             }
  326.         show_modem (lc_mstat(port));
  327.  
  328.         if (keyrdy())                       /* anything typed ? */
  329.         {
  330.             ch = getone();                  /* get input */
  331.             if (ch == ALT_X)
  332.                 return(0);
  333.             if (ch == ALT_C)
  334.                 ch = 0x03;
  335.             lc_put(port,ch);                /* xmit the char */
  336.             if (hostm || halfd)             /* local echo needed ? */
  337.             {
  338.                 vcputc(ch, msay);
  339.                 if (ch == '\r')             /* was it CR */
  340.                 {
  341.                     vcputc('\n', msay);     /* add LF */
  342.                     if (hostm)
  343.                         lc_put(port, '\n'); /* and send LF in host mode */
  344.                 }
  345.             }
  346.         }
  347.     }                                       /* while */
  348. }
  349.  
  350. void procesc()
  351. {
  352.     int ch;
  353.  
  354.  
  355.     ch = lc_getw(port);                    /* wait for actual command */
  356.  
  357.     switch (ch & 0x7f)
  358.     {
  359.         case 'A':
  360.             moveup();
  361.             break;
  362.         case 'B':
  363.             movedn();
  364.             break;
  365.         case 'C':
  366.             moveright();
  367.             break;
  368.         case 'D':
  369.             moveleft();
  370.             break;
  371.         case 'H':
  372.             at(0,0);
  373.             break;
  374.         case 'I':
  375.             bp_ESC_I();
  376.             break;
  377.         case 'J':
  378.             clreos();
  379.             break;
  380.         case 'K':
  381.             xeraeol(vc.dflt);
  382.             break;
  383.         case 'j':
  384.             erase();
  385.             break;
  386.         case 'Y':
  387.             setcursorpos();
  388.             break;
  389.     }
  390. }                                        /* procesc */
  391.  
  392. void show_modem(mdmstat)
  393. int mdmstat;
  394. {
  395.     COUNT lwin;
  396.  
  397.     if ((mdmstat & 0x0f))            /* any status change ? */
  398.     {
  399.         lwin = wselect(swin);
  400.         erase();
  401.         at(0,2);
  402.         if (mdmstat & CTS)
  403.             vcputs("CTS", ssay);
  404.         at(0,8);
  405.         if (mdmstat & DSR)
  406.             vcputs("DSR", ssay);
  407.         at(0,14);
  408.         if (mdmstat & DCD)
  409.             vcputs("DCD", ssay);
  410.         at(0,20);
  411.         if (mdmstat & RI)
  412.             vcputs("RI", ssay);
  413.         wselect(lwin);
  414.     }
  415. }                                       /* show_modem */
  416.  
  417. void clreos()
  418. {
  419.     xeraeos(vc.dflt);
  420. }                                        /* clreos */
  421.  
  422. void setcursorpos()
  423. {
  424.     int    ch;
  425.     int    x;
  426.     int    y;
  427.  
  428.     while((ch = lc_get(port)) == -1)
  429.     ;
  430.     y = (ch & 0x7f) - 32;
  431.  
  432.     while((ch = lc_get(port)) == -1)
  433.     ;
  434.     x = (ch & 0x7f) - 32;
  435.     at(y, x);
  436. }
  437.  
  438. struct text_info
  439. {
  440.     COUNT   wintop,
  441.             winbottom,
  442.             winleft,
  443.             winright,
  444.             curx,
  445.             cury;
  446. };
  447.  
  448. void gettextinfo(r)
  449. struct text_info *r;
  450. {
  451.     wcoord(mwin, &(r->wintop), &(r->winleft), &(r->winbottom), &(r->winright));
  452.     wcurspos(mwin, &(r->cury), &(r->curx));
  453. }
  454.  
  455. void moveleft()
  456. {
  457.     int scurx,
  458.         scury;
  459.     struct text_info r;
  460.  
  461.     gettextinfo(&r);
  462.     scurx = r.curx;
  463.     scury = r.cury;
  464.  
  465.     scurx--;                             /* decrement x position */
  466.     if (scurx < r.winleft)
  467.     {
  468.         scury--;
  469.         scurx = r.winright;
  470.         if (scury < r.wintop);
  471.             scury = r.winbottom;
  472.     }
  473.     at(scury, scurx);
  474. }                                        /* moveleft */
  475.  
  476. void moveright()
  477. {
  478.     int scurx,
  479.         scury;
  480.     struct text_info r;
  481.  
  482.     gettextinfo(&r);
  483.     scurx = r.curx;
  484.     scury = r.cury;
  485.  
  486.     scurx++;                             /* decrement x position */
  487.     if (scurx > r.winright)
  488.     {
  489.         scury++;
  490.         scurx = r.winleft;
  491.         if (scury > r.winbottom);
  492.             scury = r.wintop;
  493.     }
  494.     at(scury, scurx);
  495. }                                        /* moveright */
  496.  
  497. void moveup()
  498. {
  499.     int scury;
  500.  
  501.     struct text_info r;
  502.  
  503.     gettextinfo(&r);
  504.     scury = r.cury;
  505.  
  506.     scury = r.cury - 1;
  507.     if (scury < r.wintop)
  508.         scury = r.winbottom;
  509.     at(scury, r.curx);
  510. }                                        /* moveup */
  511.  
  512. void movedn()
  513. {
  514.     int scury;
  515.  
  516.     struct text_info r;
  517.  
  518.     gettextinfo(&r);
  519.     scury = r.cury;
  520.  
  521.     scury = r.cury + 1;
  522.     if (scury > r.winbottom)
  523.         scury = r.wintop;
  524.     at(scury, r.curx);
  525. }
  526.  
  527. void simtab()
  528. {
  529.     int scurx,
  530.         scury;
  531.     struct text_info r;
  532.  
  533.     gettextinfo(&r);
  534.     scurx = r.curx;
  535.     scury = r.cury;
  536.  
  537.     do
  538.         scurx++;                         /* bump x position */
  539.     while (scurx % 9);
  540.  
  541.     if (scurx > r.winright)
  542.     {
  543.         scury++;
  544.         scurx = r.winleft;
  545.         if (scury > r.winbottom);
  546.         {
  547.             at(r.winbottom, r.winright);
  548.             vcputc('\n', msay);
  549.             return;
  550.         }
  551.     }
  552.     at(scury, scurx);
  553. }                                        /* simtab */
  554.  
  555.  
  556. chgcomm()
  557. {
  558.     COUNT opt;
  559.     unsigned sbaud;
  560.     unsigned sparity;
  561.     unsigned sbits;
  562.     unsigned sstop;
  563.     int hbaud;
  564.     char hparity;
  565.     int hdata;
  566.     int hstop;
  567.  
  568. /*
  569. ** get current default settings
  570. */
  571.     sbaud = pbaud;
  572.     sparity = pparity;
  573.     sbits = pbits;
  574.     sstop = pstop;
  575.     hbaud = baud;
  576.     hparity = parity;
  577.     hdata = data;
  578.     hstop = stop;
  579.  
  580.  
  581.     while (1)
  582.     {
  583.         erase();
  584.         vcputs("-- COMM PARAMETERS --\r", msay);
  585.         sprintf(strbuf,"  (Presently %d,%c,%d,%d)\r",
  586.                 hbaud,hparity,hdata,hstop);
  587.         vcputs(strbuf, msay);
  588.         vcputs("B - change Baud Rate\r", msay);
  589.         vcputs("P - change Parity\r", msay);
  590.         vcputs("D - change Data bits\r", msay);
  591.         vcputs("S - change Stop bits\r\r", msay);
  592.         vcputs("A - Abandon Changes\r", msay);
  593.         vcputs("Q - Quit to Main Menu\r\r", msay);
  594.  
  595.         opt = getone();
  596.         opt = toupper(opt);
  597.  
  598.         switch (opt)
  599.         {
  600.             case 'A':
  601.                 return;
  602.             case 'Q':
  603.                 pbaud = sbaud;          /* reset the globals */
  604.                 pparity = sparity;
  605.                 pbits = sbits;
  606.                 pstop = sstop;
  607.                 baud = hbaud;
  608.                 parity = hparity;
  609.                 data = hdata;
  610.                 stop = hstop;
  611.                 comm_setup(port,pbaud,pparity,pbits,pstop);
  612.                 return;
  613.             case 'B':
  614.                 vcputs("1 - 110, 2 - 300, 3 - 600, 4 - 1200, 5 - 2400\r", msay);
  615.                 vcputs("6 - 4800, 7 - 9600, 8 - 19200\r", msay);
  616.                 opt = getone();
  617.                 switch (opt)
  618.                 {
  619.                     case '1': hbaud = 110;
  620.                               sbaud = 110;
  621.                               break;
  622.                     case '2': hbaud = 300;
  623.                               sbaud = 300;
  624.                               break;
  625.                     case '3': hbaud = 600;
  626.                               sbaud = 600;
  627.                               break;
  628.                     case '4': hbaud = 1200;
  629.                               sbaud = 1200;
  630.                               break;
  631.                     case '5': hbaud = 2400;
  632.                               sbaud = 2400;
  633.                               break;
  634.                     case '6': hbaud = 4800;
  635.                               sbaud = 4800;
  636.                               break;
  637.                     case '7': hbaud = 9600;
  638.                               sbaud = 9600;
  639.                               break;
  640.                     case '8': hbaud = 19200;
  641.                               sbaud = 19200;
  642.                               break;
  643.                 }
  644.                 break;
  645.             case 'P':
  646.                 vcputs("1 - NONE, 2 - EVEN, 3 - ODD, 4 - MARK, 5 - SPACE\r", msay);
  647.                 opt = getone();
  648.                 switch (opt)
  649.                 {
  650.                     case '1': hparity = 'N';
  651.                               sparity = NPARITY;
  652.                               break;
  653.                     case '2': hparity = 'E';
  654.                               sparity = EPARITY;
  655.                               break;
  656.                     case '3': hparity = 'O';
  657.                               sparity = OPARITY;
  658.                               break;
  659.                     case '4': hparity = 'M';
  660.                               sparity = MPARITY;
  661.                               break;
  662.                     case '5': hparity = 'S';
  663.                               sparity = SPARITY;
  664.                               break;
  665.                 }
  666.                 break;
  667.             case 'D':
  668.                 vcputs("Number of Data Bits (5, 6, 7, 8)\r", msay);
  669.                 opt = getone();
  670.                 switch (opt)
  671.                 {
  672.                     case '5': hdata = 5;
  673.                               sbits = BIT5;
  674.                               break;
  675.                     case '6': hdata = 6;
  676.                               sbits = BIT6;
  677.                               break;
  678.                     case '7': hdata = 7;
  679.                               sbits = BIT7;
  680.                               break;
  681.                     case '8': hdata = 8;
  682.                               sbits = BIT8;
  683.                               break;
  684.                 }
  685.                 break;
  686.             case 'S':
  687.                 vcputs("Number of Stop Bits (1, 2)\r", msay);
  688.                 opt = getone();
  689.                 switch (opt)
  690.                 {
  691.                     case '1': hstop = 1;
  692.                               sstop = STOP1;
  693.                               break;
  694.                     case '2': hstop = 2;
  695.                               sstop = STOP2;
  696.                               break;
  697.                 }
  698.                 break;
  699.         }
  700.     }
  701. }
  702.  
  703.  
  704. void send()
  705. {
  706.     COUNT twin;
  707.  
  708.     twin=wxopen(6,25,10,50,"SEND A FILE",
  709.          BORDER+BD1+ACTIVE+CURSOR+SCROLL+COOKED+NOADJ+CENTER, 0, 0);
  710.     wselect(twin);
  711.     if (xmode)
  712.         wupload();
  713.     else
  714.         xupload();
  715.     wclose(twin);
  716.     wselect(mwin);
  717. }
  718.  
  719. void recv()
  720. {
  721.     COUNT twin;
  722.  
  723.     twin=wxopen(6,25,10,50,"RECEIVE A FILE",
  724.          BORDER+BD1+ACTIVE+CURSOR+SCROLL+COOKED+NOADJ+CENTER, 0, 0);
  725.     wselect(twin);
  726.     if (xmode)
  727.         wdnload();
  728.     else
  729.         xdnload();
  730.     wclose(twin);
  731.     wselect(mwin);
  732. }
  733.  
  734. void getfname(fname)
  735. char *fname;
  736. {
  737.     erase();
  738.     atsay(0,0,"File Name: ");
  739.     empty(fname, 13);
  740.     get(fname,NULL);
  741.     readgets();
  742.     cputs("\r");
  743. }
  744.  
  745. void xupload(void)
  746. {
  747.     char fname[13];
  748.     int  fd;
  749.     unsigned char buf[1024];
  750.     unsigned char *bpos;
  751.     int toread;
  752.     int tosend;
  753.     unsigned char hdshk;
  754.     int hmode;
  755.     int result;
  756.  
  757.     getfname(fname);
  758.  
  759.     ctlc_hit = FALSE;
  760.     if (ctlc_hit == TRUE)
  761.     {
  762.         ctlc_hit = FALSE;
  763.         return;
  764.     }
  765.  
  766.     if (isblank(fname))
  767.         return;
  768.     trim(fname);
  769.  
  770.     if ((fd = open(fname, (O_RDONLY|O_BINARY))) == -1)
  771.     {
  772.         urgentmsg("WARNING", "Unable to open the file");
  773.         return;
  774.     }
  775. /*
  776. ** the file has been opened successfully...now switch the
  777. ** comport mode to 8 bits for xmodem protocol
  778. */
  779.     if (comm_setup(port,pbaud,NPARITY,BIT8,pstop) == ERR)
  780.     {
  781.         urgentmsg("ERROR","Unable to switch line mode");
  782.         return;
  783.     }
  784. /*
  785. ** begin file transmission to receiver -
  786. ** in receiver's specified mode
  787. */
  788.     if (yxmode)
  789.     {
  790.         ymodem = TRUE;
  791.         toread = 1024;
  792.     }
  793.     else
  794.     {
  795.         ymodem = FALSE;
  796.         toread = 128;
  797.     }
  798.  
  799.     while (TRUE)
  800.     {
  801.         memset(buf, 0x1a, sizeof(buf));     /* clear buff, short rec */
  802.         if ((tosend = read(fd, buf, sizeof(buf))) < 1)  /* EOF or Error */
  803.         {
  804.             lcxteot(port);            /* send end of file */
  805.             urgentmsg("SUCCESS","End of Transmission");
  806.             break;
  807.         }
  808.         bpos = buf;
  809.         while (TRUE)
  810.         {
  811.             if (tosend <= 0)
  812.                 break;                        /* block sent completely */
  813.               if (yxmode)
  814.                 if (tosend != toread)    /* short block */
  815.                 {
  816.                       toread = 128;       /* sending short */
  817.                       ymodem = FALSE;
  818.                 }
  819.             result = lcxtrec(port, bpos);
  820.             switch(result)           /* what action to take ? */
  821.             {
  822.                 case SUCCESS:
  823.                     sprintf(strbuf,"Sent record: %d",(rec-1));
  824.                     atsay(1,1,strbuf);
  825.                     eraeol();
  826.                     tosend -= toread;
  827.                     bpos += toread;
  828.                     break;
  829.                 case CAN:
  830.                     urgentmsg("WARNING","Cancel Received");
  831.                     break;
  832.                 case RETRIES:
  833.                     sprintf(strbuf, "Too Many tries...Rec %d",(rec-1));
  834.                     urgentmsg("ERROR", strbuf);
  835.                     break;
  836.                 default:
  837.                     urgentmsg("ERROR", "Fatal transmission error");
  838.                     break;
  839.             }
  840.             if (result != SUCCESS)
  841.                 break;
  842.         }                                    /* inner while */
  843.         if (result != SUCCESS)
  844.             break;
  845.     }                                          /* while TRUE */
  846.     comm_setup(port,pbaud,pparity,pbits,pstop);
  847.     close(fd);                                 /* close down the file */
  848. }
  849.  
  850. void xdnload(void)
  851. {
  852.     char fname[13];
  853.     int  fd;
  854.     unsigned char buf[1024];
  855.     unsigned char hdshk;
  856.     int bsize;
  857.     int hmode;
  858.     int result;
  859.  
  860.     ctlc_hit = FALSE;
  861.  
  862.     getfname(fname);
  863.     if (ctlc_hit == TRUE)
  864.     {
  865.         ctlc_hit = FALSE;
  866.         return;
  867.     }
  868.     if (isblank(fname))
  869.         return;
  870.     trim(fname);
  871.  
  872.     if ((fd = open(fname,
  873.          (O_WRONLY|O_CREAT|O_TRUNC|O_BINARY),(S_IREAD|S_IWRITE))) == -1)
  874.     {
  875.         urgentmsg("WARNING", "Unable to open the file");
  876.         return;
  877.     }
  878. /*
  879. ** the file has been opened successfully...now switch the
  880. ** comport mode to 8 bits for xmodem protocol
  881. */
  882.     if (comm_setup(port,pbaud,NPARITY,BIT8,pstop) == ERR)
  883.     {
  884.         urgentmsg("ERROR", "Unable to switch line mode");
  885.         return;
  886.     }
  887. /*
  888. ** setup conditions for XMODEM receive
  889. */
  890.     hdshk = CRC;                 /* use crc method */
  891.     hmode = RELAXED;                /* normal time delay used */
  892.     while (TRUE)
  893.     {
  894.         while ((result = lcxrrec(port, buf, &bsize, hmode, &hdshk)) == SUCCESS)
  895.         {
  896.             write(fd, buf, bsize);  /* dump the block */
  897.             sprintf(strbuf,"Received record: %d",(rec-1));
  898.             atsay(1,1,strbuf);
  899.             eraeol();
  900.         }
  901.         switch(result)           /* unusual conditions */
  902.         {
  903.             case DUPSEQ:
  904.                 atsay(1,1,"Duplicate record seq");
  905.                 eraeol();
  906.                 break;
  907.             case CAN:
  908.                 urgentmsg("WARNING", "Cancelled by host");
  909.                 break;
  910.             case EOT:
  911.                 urgentmsg("SUCCESS", "Normal Termination");
  912.                 break;
  913.             case TOUT:
  914.                 urgentmsg("ERROR", "SOH timeout");
  915.                 break;
  916.             case RETRIES:
  917.                 sprintf(strbuf, "Too Many tries...Rec %d",(rec-1));
  918.                 urgentmsg("ERROR", strbuf);
  919.                 break;
  920.             default:
  921.                 urgentmsg("ERROR", "Fatal transmission error");
  922.                 break;
  923.         }
  924.         if ((result != SUCCESS) && (result != DUPSEQ))
  925.             break;
  926.     }                                          /* while TRUE */
  927.  
  928.     comm_setup(port,pbaud,pparity,pbits,pstop);
  929.     close(fd);                                 /* close down the file */
  930. }
  931.  
  932. void wupload(void)
  933. {
  934.     char fname[13];
  935.     int  fd;
  936.     unsigned char buf[128];
  937.     unsigned char hdshk;
  938.     int hmode;
  939.     int result;
  940.     int nrec;                           /* returned record */
  941.  
  942.     ctlc_hit = FALSE;
  943.     getfname(fname);
  944.     if (ctlc_hit == TRUE)
  945.     {
  946.         ctlc_hit = FALSE;
  947.         return;
  948.     }
  949.     if (isblank(fname))
  950.         return;
  951.     trim(fname);
  952.  
  953.     if ((fd = open(fname,(O_RDONLY|O_BINARY))) == -1)
  954.     {
  955.         urgentmsg("ERROR", "Unable to open the file");
  956.         return;
  957.     }
  958. /*
  959. ** the file has been opened successfully...now switch the
  960. ** comport mode to 8 bits for xmodem protocol
  961. */
  962.     if (comm_setup(port,pbaud,NPARITY,BIT8,pstop) == ERR)
  963.     {
  964.         urgentmsg("ERROR", "Unable to switch line mode");
  965.         return;
  966.     }
  967.     lc_xoff(port,TRUE);                    /* turn on auto xon-xoff */
  968.  
  969. /*
  970. ** begin file transmission to receiver -
  971. ** in receiver's specified mode
  972. */
  973.     while (TRUE)
  974.     {
  975.         memset(buf, 0x1a, 128);     /* clear buff, short rec */
  976.         if (read(fd, buf, sizeof(buf)) < 1)  /* EOF or Error */
  977.             nrec = -1;              /* defines End of File */
  978.         else
  979.             nrec = 0;
  980.  
  981.         result = lwxtrec(port, buf, &nrec);
  982.  
  983.         switch(result)           /* what action to take ? */
  984.         {
  985.             case SUCCESS:
  986.                 if (nrec == -1)  /* EOF confirmed */
  987.                     urgentmsg("SUCCESS","End of Transmission");
  988.                 else
  989.                 {
  990.                     sprintf(strbuf, "Sent record: %d",rec);
  991.                     atsay(1,1,strbuf);
  992.                     eraeol();
  993.                 }
  994.                 break;
  995.             case CAN:
  996.                 urgentmsg("ERROR", "Cancel Received");
  997.                 break;
  998.             case RETRIES:
  999.                 sprintf(strbuf, "Too Many tries...Rec %d",rec);
  1000.                 urgentmsg("ERROR", strbuf);
  1001.                 break;
  1002.             case RESEND:                /* must reset file position */
  1003.                 lseek(fd, (long)(nrec * sizeof(buf)), SEEK_SET);
  1004.                 break;
  1005.             default:
  1006.                 urgentmsg("ERROR", "Fatal transmission error");
  1007.                 break;
  1008.         }
  1009.         if (result == RESEND)
  1010.             continue;
  1011.         if (result == SUCCESS)
  1012.             if (nrec == -1)             /* was EOF signalled */
  1013.                 break;
  1014.             else
  1015.                 continue;
  1016.         break;                          /* some other error */
  1017.     }                                          /* while TRUE */
  1018.     lc_xoff(port, FALSE);                         /* turn off auto xon/xoff */
  1019.     comm_setup(port,pbaud,pparity,pbits,pstop);
  1020.     close(fd);                                 /* close down the file */
  1021. }
  1022.  
  1023. void wdnload(void)
  1024. {
  1025.     char fname[13];
  1026.     int  fd;
  1027.     unsigned char buf[128];
  1028.     unsigned char hdshk;
  1029.     int hmode;
  1030.     int result;
  1031.  
  1032.     ctlc_hit = FALSE;
  1033.  
  1034.     getfname(fname);
  1035.     if (ctlc_hit == TRUE)
  1036.     {
  1037.         ctlc_hit = FALSE;
  1038.         return;
  1039.     }
  1040.     if (isblank(fname))
  1041.         return;
  1042.     trim(fname);
  1043.  
  1044.     if ((fd = open(fname,
  1045.          (O_WRONLY|O_CREAT|O_TRUNC|O_BINARY),(S_IREAD|S_IWRITE))) == -1)
  1046.     {
  1047.         urgentmsg("ERROR", "Unable to open the file");
  1048.         return;
  1049.     }
  1050. /*
  1051. ** the file has been opened successfully...now switch the
  1052. ** comport mode to 8 bits for xmodem protocol
  1053. */
  1054.     if (comm_setup(port,pbaud,NPARITY,BIT8,pstop) == ERR)
  1055.     {
  1056.         urgentmsg("ERROR", "Unable to switch line mode");
  1057.         return;
  1058.     }
  1059. /*
  1060. ** setup conditions for XMODEM receive
  1061. */
  1062.     while (TRUE)
  1063.     {
  1064.         while ((result = lwxrrec(port, buf)) == SUCCESS)
  1065.         {
  1066.             write(fd, buf, sizeof(buf));  /* dump the block */
  1067.             sprintf(strbuf, "Received record: %d", rec-1);
  1068.             atsay(1,1,strbuf);
  1069.             eraeol();
  1070.         }
  1071.         switch(result)           /* unusual conditions */
  1072.         {
  1073.             case DUPSEQ:
  1074.                 atsay(1,1,"Duplicate record seq");
  1075.                 eraeol();
  1076.                 break;
  1077.             case CAN:
  1078.                 urgentmsg("ERROR", "Cancelled by host");
  1079.                 break;
  1080.             case EOT:
  1081.                 urgentmsg("SUCCESS","Normal Termination");
  1082.                 break;
  1083.             case TOUT:
  1084.                 urgentmsg("ERROR", "SOH timeout");
  1085.                 break;
  1086.             case RETRIES:
  1087.                 sprintf(strbuf, "Too Many tries...Rec %d",(rec-1));
  1088.                 urgentmsg("ERROR", strbuf);
  1089.                 break;
  1090.             default:
  1091.                 urgentmsg("ERROR", "Fatal transmission error");
  1092.                 break;
  1093.         }
  1094.         if ((result != SUCCESS) && (result != DUPSEQ))
  1095.             break;
  1096.     }                                          /* while TRUE */
  1097.  
  1098.     comm_setup(port,pbaud,pparity,pbits,pstop);
  1099.     close(fd);                                 /* close down the file */
  1100. }
  1101.